<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class C_Facility extends CI_Controller {

 function __construct()
 {
	parent::__construct();

    $this->load->model('m_impact', '', true);	
 }
 function index($offset = ''){
	$data['facilities'] =	$this->m_impact->get_by_sql('SELECT * FROM facility ORDER BY id DESC');	 	
	$data['body'] ='admin/facility/v_list';
	$this->load->view('admin/desktop/desktop',$data);
 }
 
 /*
 show page new
 */
 function page_new(){
	$data['body']='admin/facility/v_page_new';
	$this->load->view('admin/desktop/desktop',$data);
 }
 /*
 Insert data to DB
 */
 function save_new ($type = FALSE){
	
	$data_arr = array(
						'name'			=> $this->input->post('name'),
						'description'			=> $this->input->post('description'),   
						'icon' 		=> $this->input->post('image_eng'), 
						'image' 		=> $this->input->post('image_fr')  
					 );
	 $result =	$this->m_impact->insert('facility', $data_arr, '');
	 if($result){
		 $this->session->set_userdata('message', $this->connect->message("facility has been created!", "success"));
	 }else{
		 $this->session->set_userdata('message', $this->connect->message("facility has been created!", "warning"));
	 }
	 
	 redirect(site_url().'admin/c_facility/page_new', 'location', 302);
 }
 /*
 page edit
 */
 function page_edit($id = false){
	 $id = $id;

	 $where = array('id' => $id);
	 $data['facility']	=	array_shift($this->m_impact->get_by_fields('facility',$where,'id'));
	 
	 $data['body']='admin/facility/v_page_edit';
	 $this->load->view('admin/desktop/desktop',$data);
 }
 
 /*
 page update data
 */
 function update(){
	$id = $this->input->post('id');
	$where	= array('id' => $id);
	$data_arr = array(
						'name'			=> $this->input->post('name'),
						'description'	=> $this->input->post('description'),   
						'icon' 		=> $this->input->post('image_eng'), 
						'image' 		=> $this->input->post('image_fr')  
					 );
	$result	=	$this->m_impact->update_field('facility', $data_arr, $where);
	
	if($result){
		$this->session->set_userdata('message', $this->connect->message("facility has been updated!", "success"));
	}else{
		$this->session->set_userdata('message', $this->connect->message("facility has been updated!", "warning"));
	}

	redirect(site_url().'admin/c_facility', 'location', 302);
 }
 
 /*
 page update data
 */
 function delete($id){	
	$where = array('id' => $id);
	$facility	=	array_shift($this->m_impact->get_by_fields('facility',$where,'id'));
	if($facility){
	   @unlink('./upload/facility/'.$facility['icon']);
	   @unlink('./upload/facility/'.$facility['image']);
	}
	
	$result	=	$this->m_impact->delete('facility', $where);
	if($result){
	   $this->session->set_userdata('message', $this->connect->message("facility has been deleted!", "success"));
	}else{
	   $this->session->set_userdata('message', $this->connect->message("facility has been deleted!", "warning"));
	} 
	
	redirect(site_url().'admin/c_facility', 'location', 302);
 }
 
}
// end c_promotion

?>
